<?php

define("GC_WEB", 1);

require __DIR__ . '/../init.php';

$module = isset($_GET['module']) ? $_GET['module'] : '';
$action = isset($_GET['action']) ? $_GET['action'] : '';


$controllerPath = __DIR__ . "/../include/controller/";
$viewPath = __DIR__ . "/../include/view/";

$file = "$controllerPath/$module/$action.php";
if(!is_file($file)) {
	$action = "index";
	$file = "$controllerPath/$module/$action.php";
	if(!is_file($file)) {
		$module = "index";
		$action = "index";
	}
}

$file = "$controllerPath/$module/$action.php";
$viewFile = "$viewPath/$module/$action.php";

if($module != "init") {
	try {
		$db = DB::getInstance($_DB_CONFIG);
	} catch (Exception $e) {
		Response::redirect("/?module=init");
	}
}

$data = array();
try {
	$data = require($file);
} catch (Exception $e) {
	var_dump($e);
}
$responseType = Request::get("response_type");

if($responseType == "json") {
	echo json_encode($data);
} else {
	include($viewFile);
}
